class Solution:
    def stoneGame(self, piles: list) -> bool:
        def play(piles, start, end, player, score1, score2, cache):
            if start > end:
                return score1 > score2
            if (start, end) in cache:
                return cache[(start, end)]

            if player == 1:
                res = play(piles, start + 1, end, -player,
                           score1 + piles[start], score2, cache) or play(
                               piles, start, end - 1, -player,
                               score1 + piles[end], score2, cache)
            else:
                res = play(piles, start + 1, end, -player, score1,
                           score2 + piles[start], cache) or play(
                               piles, start, end - 1, -player, score1,
                               score2 + piles[end], cache)
            cache[(start, end)] = res
            return res

        return play(piles, 0, len(piles) - 1, 1, 0, 0, {})
